//
//  VedioModel.m
//  DMT_VR
//
//  Created by xin on 2016/10/24.
//  Copyright © 2016年 Elephant-Parade. All rights reserved.
//

#import "VedioModel.h"
#import "MJExtension.h"
// 缓存主目录
#define DownloadCachesDirectory [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"DownloadList"]

#define DownloadListPath [DownloadCachesDirectory stringByAppendingPathComponent:@"download.plist"]

@interface VedioModel()

@end

@implementation VedioModel

MJExtensionCodingImplementation

+ (NSArray *)readDiskAllCache
{
    NSLog(@"DownloadListPath=%@",DownloadListPath);
    //通过本地路径plist创建model数组
    return [NSArray arrayWithContentsOfFile:DownloadListPath];
}

+ (BOOL)saveCache:(VedioModel *)model
{
    BOOL save = NO;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:DownloadCachesDirectory]) {
        [fileManager createDirectoryAtPath:DownloadCachesDirectory withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    NSMutableArray * array = [[NSMutableArray alloc] initWithArray:[VedioModel readDiskAllCache]];
    
    //判断是否存在数组字典中
    for (NSDictionary * dic in array) {
        if ([dic objectForKey:model.vediourl]) {
            NSLog(@"已存在");
            save = YES;
        }
    }
    
    if (!save) {
        //把模型转成字典存到本地plist
        NSDictionary * modelDic = model.mj_keyValues;
        [array addObject:@{model.vediourl:modelDic}];
        
        save = [array writeToFile:DownloadListPath atomically:YES];
    }
    
    return save;
}

+ (void)deleteCache:(VedioModel *)model
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:DownloadCachesDirectory]) {
        [fileManager createDirectoryAtPath:DownloadCachesDirectory withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    NSMutableArray * array = [[NSMutableArray alloc] initWithArray:[VedioModel readDiskAllCache]];
    
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj objectForKey:model.vediourl]) {
            *stop = YES;
            if (*stop == YES) {
                [array removeObject:obj];
            }
        }
    }];
    
    [array writeToFile:DownloadListPath atomically:YES];
}

@end

//
//- (id)copyWithZone:(NSZone *)zone
//{
//    VedioModel * copy = [[[self class] allocWithZone:zone] init];
//    copy.vedioid = self.vedioid;
//    copy.vediourl = self.vediourl;
//    copy.imgurl = self.imgurl;
//    copy.shareurl = self.shareurl;
//    copy.title_ch = self.title_ch;
//    copy.title_en = self.title_en;
//    copy.type = self.type;
//    copy.vediotime = self.vediotime;
//    copy.info = self.info;
//    
//    return copy;
//}
//
//- (id)mutableCopyWithZone:(NSZone *)zone
//{
//    VedioModel * copy = [[[self class] allocWithZone:zone] init];
//    copy.vedioid = self.vedioid;
//    copy.vediourl = self.vediourl;
//    copy.imgurl = self.imgurl;
//    copy.shareurl = self.shareurl;
//    copy.title_ch = self.title_ch;
//    copy.title_en = self.title_en;
//    copy.type = self.type;
//    copy.vediotime = self.vediotime;
//    copy.info = self.info;
//    
//    return copy;
//}
//
